import React, { PureComponent } from "react";
import { ScrollView, View, NativeModules } from "react-native";
import { Button, Text, Input } from "react-native-tengits-elements";
import RNFetchBlob from "rn-fetch-blob";
import UpgradeLibrary from "../libs/Upgrade";
import XLog from "../libs/XLog";

export default class DebugPage extends React.PureComponent {

    constructor() {
        super();
        this.state = {
            usb:[]
        }
    }

    async componentDidMount() {

    }

    render() {
        return <ScrollView style={{flex:1, backgroundColor:'white', padding:30}}>
                <View style={{flex:1, flexDirection:'row', justifyContent:'space-between', marginBottom: 60}}>
                <Button
                    title="安装app"
                    onPress={async()=>{
                        let path = await UpgradeLibrary.getDownloadPath();
                        console.log("apk path:", path);
                        //    /storage/emulated/0/TenRoad/document/mqttmonitor-release.apk
                        // https://code-push-1255466169.cos.ap-guangzhou.myqcloud.com/mqttmonitor-release.apk
                        UpgradeLibrary.doNewVersionUpdate(
                            "/storage/emulated/0/TenRoad/document/mqttmonitor-release.apk", true);
                    }}
                    />
                <Button
                    title="上传"
                    onPress={async()=>{
                        let param = {date:"20211207", url:"http://192.168.2.117:7000/upload"}
                        let path = await XLog.getLogPath();
                        let log_full_path = path + "/" + "monitor_" + param.date + ".xlog";
                        RNFetchBlob.fs.exists(log_full_path)
                        .then((exist) => {
                            if(exist) {
                                RNFetchBlob.fetch('POST', param.url, {
                                    'Content-Type' : 'octet-stream',
                                }, [
                                    { name: "file", filename: "monitor_" + param.date + ".xlog", type: 'file', 
                                    data: RNFetchBlob.wrap(log_full_path) },
                                    //... 可能还会有其他非文件字段{name:'字段名',data:'对应值'}
                                ])
                                .then((res) => {
                                    console.log("upload ret:",res);
                                })
                                .catch((err) => {
                                    console.log("err: ", err);
                                    
                                })
                            } else {
                                console.log(`${log_full_path} file not exist`);
                            }
                        })
                        .catch(() => { 
                            console.log(`${log_full_path} file exist exception`);
                        });
                    }}
                    />
                    <Button
                    title="usb"
                    onPress={async()=>{
                        let info = await NativeModules.SerialPortAPI.getUsbInfo();
                        const{usb} = this.state;
                        let newInfo = (usb||[]).concat(info);
                        console.log("info:",info, newInfo)
                        this.setState({
                            usb: newInfo,
                        })
                    }}
                    />
                </View>
                <View style={{flex:1}}>
                    {(this.state.usb||[]).map((item, index)=>{
                        return <Text key={"t_" + index}>{JSON.stringify(item)}</Text>
                    })}
                </View>
        </ScrollView>
    }

}